package com.ibeeking.found.open.b.rest.service.mq;

import com.ibeeking.found.common.constants.WxCpServiceTypeConstant;
import com.ibeeking.found.common.entity.MqMsgEventParam;
import com.ibeeking.found.common.entity.open.OpenAuthDTO;
import com.ibeeking.found.common.entity.open.OpenAuthQuery;
import com.ibeeking.found.common.enums.OpenApiConfigEnum;
import com.ibeeking.found.common.enums.WxCpAppTypeEnum;
import com.ibeeking.found.cop.api.common.param.MsgEventDataTalkParam;
import com.ibeeking.gmc.global.api.feign.GlobalOpenFeignClient;
import com.ibeeking.found.open.b.rest.service.IMqService;
import com.ibeeking.nematos.exception.BusinessException;
import com.ibeeking.nematos.log.utils.LogUtils;
import com.ibeeking.nematos.utils.json.JsonUtils;
import com.ibeeking.nematos.utils.result.ResponseResult;
import me.chanjar.weixin.cp.bean.message.WxCpXmlMessage;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class WxCpDataTalkMsgEventHandler {

    @Resource
    private IMqService mqServiceImpl;

    @Resource
    private GlobalOpenFeignClient globalOpenFeignClient;

    //配置了客户联系功能的成员添加外部联系人同意进行聊天内容存档时，回调该事件。
    public void processmsgAuditApproveMsgEvent(WxCpXmlMessage wxCpXmlMessage){
        String suiteId = wxCpXmlMessage.getAgentId();
        String authCorpId = "";

        OpenAuthDTO wxOpenAuthDTO = queryWxOpenAuthListByComponentTypeAndAppType();
        if (null == wxOpenAuthDTO) {
            throw new BusinessException("无法获取开发平台参数信息");
        }
        MqMsgEventParam msgEventParam = new MqMsgEventParam();
        msgEventParam.setFromUserName(wxCpXmlMessage.getFromUserName());
        msgEventParam.setToUserName(wxCpXmlMessage.getToUserName());
        msgEventParam.setEvent(wxCpXmlMessage.getEvent());
        msgEventParam.setTenantId(wxOpenAuthDTO.getTenantId().toString());
        msgEventParam.setMsgType(wxCpXmlMessage.getMsgType());
        msgEventParam.setChangeType(wxCpXmlMessage.getChangeType());
        msgEventParam.setCreateTime(wxCpXmlMessage.getCreateTime());
        MsgEventDataTalkParam dataTalkParam = new MsgEventDataTalkParam();
        dataTalkParam.setTenantId(wxOpenAuthDTO.getTenantId().toString());
        dataTalkParam.setEmployeeCode(wxCpXmlMessage.getUserId());
        dataTalkParam.setFriendsCode(wxCpXmlMessage.getExternalUserId());
        dataTalkParam.setWelcomeCode(wxCpXmlMessage.getWelcomeCode());
        msgEventParam.setParams(JsonUtils.toJsonStr(dataTalkParam));
        LogUtils.info(">>>>>>> DataTalk param:{}", JsonUtils.toJsonStr(msgEventParam));
        mqServiceImpl.sendMsgEventMessage(msgEventParam);
    }
    /*
    * 当企业收到或发送新消息时，企业微信可以以事件的形式推送到企业指定的url。回调间隔为15秒，在15秒内若有消息则触发回调
    */
    public void processNotifySyncMsgEvent(WxCpXmlMessage wxCpXmlMessage){
        String suiteId = wxCpXmlMessage.getAgentId();
        String authCorpId = "";

        OpenAuthDTO wxOpenAuthDTO = queryWxOpenAuthListByComponentTypeAndAppType();
        if (null == wxOpenAuthDTO) {
            throw new BusinessException("无法获取开发平台参数信息");
        }
        MqMsgEventParam msgEventParam = new MqMsgEventParam();
        msgEventParam.setFromUserName(wxCpXmlMessage.getFromUserName());
        msgEventParam.setToUserName(wxCpXmlMessage.getToUserName());
        msgEventParam.setEvent(wxCpXmlMessage.getEvent());
        msgEventParam.setTenantId(wxOpenAuthDTO.getTenantId().toString());
        msgEventParam.setMsgType(wxCpXmlMessage.getMsgType());
        msgEventParam.setChangeType(wxCpXmlMessage.getChangeType());
        msgEventParam.setCreateTime(wxCpXmlMessage.getCreateTime());
        MsgEventDataTalkParam dataTalkParam = new MsgEventDataTalkParam();
        dataTalkParam.setTenantId(wxOpenAuthDTO.getTenantId().toString());
        //dataTalkParam.setAgentID(wxCpXmlMessage.getAgentId());
        msgEventParam.setParams(JsonUtils.toJsonStr(dataTalkParam));
        LogUtils.info(">>>>>>> DataTalk param:{}", JsonUtils.toJsonStr(msgEventParam));
        mqServiceImpl.sendMsgEventMessage(msgEventParam);
    }

    private OpenAuthDTO queryWxOpenAuthListByComponentTypeAndAppType() {
        OpenAuthQuery query = new OpenAuthQuery();
        query.setConfigType(OpenApiConfigEnum.OPEN_CONFIG_TYPE_WECHAT_CP.getCode());
        query.setConfigSubType(WxCpAppTypeEnum.WECHAT_CP_APP_TYPE_SB.getCode());
        query.setAppType(WxCpServiceTypeConstant.CP_TYPE_SB);
        ResponseResult<OpenAuthDTO> responseResult = globalOpenFeignClient.queryOpenAuthOne(query);
        return responseResult.getData();
    }

}
